import UnoCSS from 'unocss/vite'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { viteMockServe } from 'vite-plugin-mock'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
  console.log(command, mode)
  return {
    plugins: [
      vue(),
      UnoCSS(),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia'],
        // api
        resolvers: [ElementPlusResolver()],
        eslintrc: {
          // 给eslint生成的配置 只需设置true一次 生成一次.eslintrc-auto-import.json后设置为false
          // 还需在tsconfig.app.json中 compilerOptions.types加入 "./components.d.ts"  "./auto-imports.d.ts"
          enabled: false
        }
      }),
      Components({
        // 解析组件
        resolvers: [ElementPlusResolver()],
        // 目录下组件可以自动加载
        dirs: ['src/components', 'src/layout/components', 'src/**/components']
      }),
      ElementPlus({}),
      viteMockServe({
        mockPath: 'mock',
        enable: command === 'serve',
        logger: true
      })
    ],
    server: {
      proxy: {
        '/dev-api': {
          target: 'http://localhost:3000',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/dev-api/, '/api')
        }
      }
    },
    resolve: {
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, 'src')
        },
        {
          find: '@v',
          replacement: resolve(__dirname, 'src/views')
        },
        {
          find: '@c',
          replacement: resolve(__dirname, 'src/components')
        },
        {
          find: '@h',
          replacement: resolve(__dirname, 'src/hooks')
        },
        {
          find: '@u',
          replacement: resolve(__dirname, 'src/utils')
        }
      ]
    }
  }
})
